//
//  ChatConversationListViewController.m
//  HelloCordova
//
//  Created by wanghongfei2016 on 16/8/22.
//
//

#import "ChatConversationListViewController.h"
#import <JMessage/JMSGUser.h>
#import "JCHATConversationListViewController.h"

@interface ChatConversationListViewController ()

@property (nonatomic,strong)UIButton *loginBtn;
@property (nonatomic, strong)UIButton *logoutBtn;

@end

@implementation ChatConversationListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.title = @"艺人邦";
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    [self updateUI];
    // Do any additional setup after loading the view.
}

-(void)updateUI{
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBtn.frame = CGRectMake(0, 0, 80, 80);
    _loginBtn.backgroundColor = [UIColor blueColor];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:_loginBtn];
    [_loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _logoutBtn.frame = CGRectMake(0, 100, 80, 80);
    _logoutBtn.backgroundColor = [UIColor blueColor];
    [_logoutBtn setTitle:@"退出" forState:UIControlStateNormal];
    [self.view addSubview:_logoutBtn];
    [_logoutBtn addTarget:self action:@selector(logoutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)logoutBtnClick:(UIButton *)sender
{
    NSLog(@"点击了退出按钮");
    [JMSGUser logout:^(id resultObject, NSError *error) {
        if (error == nil) {
            NSLog(@"退出成功");
            [self alertWithTitle:@"退出成功"];
        }else
        {
            NSLog(@"退出错误error = %@",error);
            [self alertWithTitle:@"退出错误"];
        }
        
    }];
    
}

-(void)loginBtnClick:(UIButton *)sender
{
    NSLog(@"点击了登录按钮");
    
    
    [JMSGUser loginWithUsername:@"111111" password:@"111111" completionHandler:^(id resultObject, NSError *error) {
        
        if (error == nil) {
            NSLog(@"登录成功");
            JCHATConversationListViewController *conversationListVC = [JCHATConversationListViewController new];
            [self.navigationController pushViewController:conversationListVC animated:YES];        }else
        {
            NSLog(@"登录error = %@",error);
            [self alertWithTitle:@"登录错误"];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)alertWithTitle:(NSString *)title
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:action];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
